//
//  MainTabBarController.m
//  ShareParking
//
//  Created by STAR on 2017/8/3.
//  Copyright © 2017年 zjw. All rights reserved.
//

#import "MainTabBarController.h"
#import "AboutMeViewController.h"
#import "HomePageViewController.h"
#import "WorkCalendarViewController.h"
#import "BaseNavigationVC.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>
{
    int noReadNum;
}

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    noReadNum =0;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(badgeNumChange) name:@"badgeNum" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(badgeNumClear) name:@"clearNum" object:nil];
    [self setRootVC];
    
}

-(void)badgeNumChange{
    
    if (self.selectedIndex !=0) {
        NSLog(@"no:%i",noReadNum);
        noReadNum =  noReadNum+1;
        NSLog(@"no:%i",noReadNum);
        [[[self tabBar] items][0] setBadgeValue:[NSString stringWithFormat:@"%i",noReadNum]];
        
    }
}

-(void)badgeNumClear{
    noReadNum =0;
    [[[self tabBar] items][0] setBadgeValue:nil];
}
//设置框架，根控制器
- (void)setRootVC
{
    UIViewController *vc = [NSClassFromString(@"ZYClientVC") new];
    //    AboutMeViewController* vc = [[AboutMeViewController alloc]init];
    vc.navigationItem.title = @"我的";

    HomePageViewController* newsVC = [[HomePageViewController alloc]init];
    newsVC.navigationItem.title = @"今日客户";
//  MineViewController* mineVC = [[MineViewController alloc]init];
    WorkCalendarViewController *workVC = [[WorkCalendarViewController alloc]init];
    workVC.navigationItem.title = @"工作日历";
    
    BaseNavigationVC* SecondVC = [[BaseNavigationVC alloc]initWithRootViewController:workVC];
    BaseNavigationVC* firstVC = [[BaseNavigationVC alloc]initWithRootViewController:newsVC];
    BaseNavigationVC* ThridVC = [[BaseNavigationVC alloc]initWithRootViewController:vc];
    [self setViewControllers:@[firstVC,SecondVC,ThridVC]];
    //self.delegate = self;
    //self.tabBar.tintColor = [UIColor color_main_50D2C2];
    [self customizeTabBarForController:self];
    
}

//设置控制器
- (void)customizeTabBarForController:(MainTabBarController *)tabBarController
{
    //
    NSArray *tabBarItemImages = @[@"first", @"second",@"third"];
    NSInteger index = 0;
    
    for (UIViewController *vc in self.viewControllers) {
        
        //        //        NSLog(@"index=%ld---frame=%f",(long)index,item.frame.size.width);
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabBarItemImages objectAtIndex:index]]];
        vc.tabBarItem.selectedImage = selectedimage;
        vc.tabBarItem.image = unselectedimage;
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
        vc.tabBarItem.tag = 100+index;
        index++;
        
    }
    self.selectedIndex=0;
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag - 100 == 0)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"clearNum" object:nil];
    }
    NSLog(@"====");
}

//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    return YES;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
*/

@end
